﻿// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <curl/curl.h>
#include <iostream>
#include <string>

// 写数据的回调函数
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* s) {
	size_t newLength = size * nmemb;
	size_t oldLength = s->size();
	s->resize(oldLength + newLength);
	std::copy((char*)contents, (char*)contents + newLength, s->begin() + oldLength);
	return newLength;
}

int main() {
	CURL* curl;
	CURLcode res;

	std::string readBuffer;

	curl_global_init(CURL_GLOBAL_DEFAULT);

	curl = curl_easy_init();
	if (curl) {
		curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com"); 
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);

		res = curl_easy_perform(curl);

		if (res != CURLE_OK) {
			std::cerr << "CURL error: " << curl_easy_strerror(res) << std::endl;
		}
		else {
			std::cout << "网页内容: " << readBuffer << std::endl;
		}

		// 清理 CURL 资源
		curl_easy_cleanup(curl);
	}

	// 全局清理
	curl_global_cleanup();

	// 清理字符串缓冲区（如果后续不再使用）
	readBuffer.clear();

	return 0;
}
